package com.dy.scs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.scs.mapper.CourseMapper;
import com.dy.scs.mapper.SCMapper;
import com.dy.scs.pojo.*;
import com.dy.scs.service.CourseService;
import com.dy.scs.service.SCService;
import com.dy.scs.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SCServiceImpl extends ServiceImpl<SCMapper, SC> implements SCService {
    @Autowired
    private SCMapper scMapper;
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseService courseService;

    @Override
    public Page<SC> pages(Integer pages, Integer pageSize,String key,String sortBy,String sortType) {
        Page<SC> page = new Page<>(pages,pageSize);
        QueryWrapper<SC> scQueryWrapper = new QueryWrapper<>();
        if(sortBy!=null&&!sortBy.isEmpty()){
            if("asc".equalsIgnoreCase(sortType))
                scQueryWrapper.orderByAsc(sortBy);
            if("desc".equalsIgnoreCase(sortType))
                scQueryWrapper.orderByDesc(sortBy);
        }

        if(key!=null){
            scQueryWrapper.like("sno",key)
                    .or().like("cno",key)
                    .or().like("grade",key);

        }
        return page(page,scQueryWrapper);
    }

    @Override
    public Result<List<gradeVO>> getSCBycno(String cno) {
        List<gradeVO> list=new ArrayList<>();
        Course course=courseService.getById(cno);
        if(course==null){
            return Result.error("");
        }
        String cname = course.getCname();
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("cno",cno);
        return null;
    }

    @Override
    public Result<List<gradeVO>> getBySC(String sno) {
        List<gradeVO> list = new ArrayList<>();
        Student student=studentService.getById(sno);
        if(student==null){
            return Result.error("");
        }

        String sname = student.getSname();
        QueryWrapper<SC> scQueryWrapper = new QueryWrapper<>();
        scQueryWrapper.eq("sno",sno);

        List<SC> scs = scMapper.selectList(scQueryWrapper);
        if(scs==null||scs.isEmpty()){
            return Result.error("");
        }

        List<String> cnos = scs.stream().map(SC::getCno).toList();
        List<Course> courses = courseMapper.selectBatchIds(cnos);
        Map<String, Course> courseMap = courses.stream().collect(Collectors.toMap(Course::getCno, course -> course));

        scs.forEach(sc -> {
            gradeVO gradeVO = new gradeVO();
            gradeVO.setSname(sname);
            Course course = courseMap.get(sc.getCno());
            String cname = course.getCname();
            gradeVO.setCname(cname);
            gradeVO.setGrade(sc.getGrade());
            list.add(gradeVO);
        });

        return Result.success(list);
    }

    @Override
    public SC getSCBy(String cno, String sno) {
        QueryWrapper<SC> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("cno",cno).eq("sno",sno);
        return getOne(queryWrapper);
    }

    @Override
    public void deletes(List<SCpair> sCpairs) {
        scMapper.deletes(sCpairs);
    }



//    @Override
//    public String getBySC(String sno, String cno) {

//    }
}
